﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;

public partial class IFRS_WIN03_1S : PageBase
{
    #region === Property ===

    private Paging paging = new Paging();
    private SubsidyDeposit model = new SubsidyDeposit();

    #endregion

    #region === Event ===

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewMenu();
            BindControl();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSEARCH_Click(object sender, EventArgs e)
    {
        LoadData(1);
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Index/Index.aspx");
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN03_4A.aspx");
    }

    protected void lnkDeleteItems_Click(object sender, EventArgs e)
    {
        var strKey = ((LinkButton)sender).Attributes["SubsidyDepositID"];
        
        model.SubsidyDepositID = strKey.AsInt32();
        model.SetStandardField();

        var result = new SPR_SUBSIDYDEPOSIT().DELETE(model);
        if (result > 0)
        {
            Utility.AlertMessage(Page, Resources.ifrsResource.DeleteCompleted);
            LoadData(1);
        }
        else
        {
            Utility.AlertMessage(Page, Resources.ifrsResource.DeleteFailed);
        }
    }
    protected void lnkCopyItems_Click(object sender, EventArgs e)
    {
        var strKey = ((LinkButton)sender).Attributes["SubsidyDepositID"];
        Response.Redirect(ResolveUrl(string.Format("~/IFRS/WIN03_4A.aspx?&id={0}", strKey)));
    }
    protected void lnkEditItems_Click(object sender, EventArgs e)
    {
        var strKey = ((LinkButton)sender).Attributes["SubsidyDepositID"];
        Response.Redirect(ResolveUrl(string.Format("~/IFRS/WIN03_2E.aspx?&id={0}", strKey)));
    }
    protected void lnkViewItems_Click(object sender, EventArgs e)
    {
        var strKey = ((LinkButton)sender).Attributes["SubsidyDepositID"];
        Response.Redirect(ResolveUrl(string.Format("~/IFRS/WIN03_3V.aspx?&id={0}", strKey)));
    }

    #endregion

    #region === Method ===

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN03_1S;
    }
    private void BindControl()
    {
        ddlDataType.SetData(DDL_TYPE.SubsidyDeposit, string.Empty, true, false);
        ddlStatus.SetData(DDL_TYPE.StatusConfirmed,string.Empty, true, false);
    }
    private void LoadData(int pageIndex)
    {
        var paging = new Paging();
        paging = Page.LoadViewState(paging, ViewState) ?? new Paging();
        paging.Sorting = paging.Sorting ?? "";
        paging.PageSize = 10;
        paging.PageIndex = pageIndex;

        model.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
        model.CostCenter = txtCostCenter.Text;

        if (!string.IsNullOrEmpty(ddlDataType.SelectedValue))
            model.DataTypeID = ddlDataType.SelectedValue.AsInt32();
        else
            model.DataTypeID = null;

        if (!string.IsNullOrEmpty(ddlStatus.SelectedValue))
            model.IsConfirmed = ddlStatus.SelectedValue == "1";
        else
            model.IsConfirmed = null;

        var recordCount = 0;
        //var dt = new SPR_SUBSIDYDEPOSIT().SEARCH_ALL(model, paging, SessionManager.IFRS_ALLBRANCH, SessionManager.IFRS_COST_CENTER, ref recordCount);
        var dt = new SPR_SUBSIDYDEPOSIT().SEARCH_ALL(model, paging, true, SessionManager.IFRS_COST_CENTER, ref recordCount);
        if (dt != null && dt.Rows.Count > 0)
        {
            gvSERACH.DataSource = dt;
            gvSERACH.DataBind();
        }
        else
        {
            gvSERACH.DataSource = null;
            gvSERACH.DataBind();
        }

        PopulatePager(recordCount, pageIndex);
        Page.SaveViewState(paging, ViewState);
    }

    #endregion

    #region === GridView & Paging ===

    protected void Page_Changed(object sender, EventArgs e)
    {
        var linkButton = sender as LinkButton;
        if (linkButton != null)
        {
            var pageIndex = int.Parse(linkButton.CommandArgument);
            LoadData(pageIndex);
        }
    }

    private string SortingExpression
    {
        get
        {
            return ViewState["SortingExpression"] != null
                ? Convert.ToString(ViewState["SortingExpression"])
                : string.Empty;
        }
        set { ViewState["SortingExpression"] = value; }
    }
    private SortDirection? SortingDirection
    {
        get
        {
            return ViewState["SortDirection"] != null
                ? (SortDirection?)ViewState["SortDirection"]
                : null;
        }
        set { ViewState["SortDirection"] = value; }
    }

    private void PopulatePager(int recordCount, int currentPage)
    {
        var dblPageCount = (double)((decimal)recordCount / 10);
        var pageCount = (int)Math.Ceiling(dblPageCount);

        var pages = new List<ListItem>();
        if (pageCount > 0)
        {
            pages.Add(new ListItem("First", "1", currentPage > 1));
            for (var i = 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(Convert.ToString(i), Convert.ToString(i), i != currentPage));
            }

            pages.Add(new ListItem("Last", Convert.ToString(pageCount), currentPage < pageCount));
        }

        repPager.DataSource = pages;
        repPager.DataBind();
    }

    protected void gvSERACH_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex > -1 && e.Row.RowType == DataControlRowType.DataRow)
        {
            var lnkDelete = (LinkButton)e.Row.FindControl("lnkDelete");
            var lnkEdit = (LinkButton)e.Row.FindControl("lnkEdit");

            var drv = (e.Row.DataItem as DataRowView);
            if (drv != null && (bool)drv.Row["IsConfirmed"])
            {
                lnkDelete.Visible = false;
                lnkEdit.Visible = false;
            }
            else
            {
                lnkDelete.Visible = true;
                lnkEdit.Visible = true;
            }

            e.Row.SetRowAttribute("SubsidyDepositID");
            e.Row.SetLinkAttribute("lnkCopy", "SubsidyDepositID");
            e.Row.SetLinkAttribute("lnkView", "SubsidyDepositID");
            e.Row.SetLinkAttribute("lnkEdit", "SubsidyDepositID");
            e.Row.SetLinkAttribute("lnkDelete", "SubsidyDepositID");
        }
    }
    protected void gvSERACH_Sorting(object sender, GridViewSortEventArgs e)
    {
        paging = Page.LoadViewState(paging, ViewState);

        SortingExpression = e.SortExpression.ToUpper();
        SortingDirection = SortingDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;

        paging.Sorting = e.Sorting(SortingExpression, SortingDirection);
        Page.SaveViewState(paging, ViewState);

        LoadData(paging.PageIndex);
    }

    #endregion

}